package com.mlog.njyjy.ndop.msg.admin.controller;

import com.mlog.njyjy.mp.starter.maws.response.Resp;
import com.mlog.njyjy.ndop.msg.admin.service.AlarmManagerService;
import com.mlog.njyjy.ndop.msg.common.domain.DelayMonitor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zyz
 * @version v1.0, 2020/8/17
 */
@Api(value = "消息流程监控项管理")
@RestController
@CrossOrigin
@RequestMapping("/api/delayMonitors")
public class AlarmManagerController {

    @Autowired
    private AlarmManagerService service;

    @ApiOperation(value = "列出所有监控项", tags = "消息流程监控项管理")
    @GetMapping("")
    public Resp list() {
        List<DelayMonitor> monitorList = service.list();
        if (monitorList != null) {
            return Resp.of(monitorList);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "根据指定的业务类型列出所有监控项", tags = "消息流程监控项管理")
    @GetMapping("/{typeName}")
    public Resp listByType(@PathVariable String typeName) {
        List<DelayMonitor> monitorList = service.listByType(typeName);
        if (monitorList != null) {
            return Resp.of(monitorList);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "根据指定的业务类型列出所有监控项", tags = "消息流程监控项管理")
    @GetMapping("/{typeName}/{times}")
    public Resp listByTypeAndTimes(@PathVariable String typeName, @PathVariable String times) {
        List<DelayMonitor> monitorList = service.listByTypeAndTimes(typeName, times);
        if (monitorList != null) {
            return Resp.of(monitorList);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "保存监控项", tags = "消息流程监控项管理")
    @PostMapping("")
    public Resp save(@RequestBody DelayMonitor delayMonitor) {
        boolean succeed = service.save(delayMonitor);
        return succeed ? Resp.OK : Resp.FAIL;
    }

    @ApiOperation(value = "导入监控项配置文件", tags = "消息流程监控项管理")
    @PostMapping("/import")
    public Resp importFile(MultipartFile file) {
        try {
            List<DelayMonitor> monitorList = service.importFile(file.getBytes());
            if (monitorList != null) {
                return Resp.of(monitorList);
            }
            return Resp.FAIL;
        } catch (IOException e) {
            return Resp.fail(500, e.getLocalizedMessage());
        }
    }

    @ApiOperation(value = "同步监控项配置", tags = "消息流程监控项管理")
    @PostMapping("/sync")
    public Resp sync() {
        List<DelayMonitor> monitorList = service.syncDelayMonitor();
        if (monitorList != null) {
            return Resp.of(monitorList);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "启用监控项", tags = "消息流程监控项管理")
    @PostMapping("/enabled")
    public Resp enabled(@RequestParam String ids) {
        boolean succeed = service.changeStatus(1, ids);
        return succeed ? Resp.OK : Resp.FAIL;
    }

    @ApiOperation(value = "取消启用监控项", tags = "消息流程监控项管理")
    @PostMapping("/disabled")
    public Resp disabled(@RequestParam String ids) {
        boolean succeed = service.changeStatus(0, ids);
        return succeed ? Resp.OK : Resp.FAIL;
    }

    @ApiOperation(value = "下载", tags = "消息流程监控项管理")
    @GetMapping("/download")
    public Resp download() {
        return Resp.FAIL;
    }
}
